package com.sh.caryr.erp.controller.product;


import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sh.caryr.erp.entity.PageResultMessage;
import com.sh.caryr.erp.entity.ResultMessage;
import com.sh.caryr.erp.entity.product.CombDetail;
import com.sh.caryr.erp.entity.product.CombNum;
import com.sh.caryr.erp.exception.ResultCode;
import com.sh.caryr.erp.entity.user.User;
import com.sh.caryr.erp.exception.RestServiceException;
import com.sh.caryr.erp.service.ICombDetailService;
import com.sh.caryr.erp.service.ICombNumService;
import com.sh.caryr.erp.service.IProductService;
import com.sh.caryr.erp.util.MyBatisUtil;
import com.sh.caryr.erp.util.UtilityTool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套件详情表 前端控制器
 * </p>
 *
 * @author milkeeper
 * @since 2017-11-22
 */
@RestController
@RequestMapping("/api/combDetail")
public class CombDetailController {
    @Autowired
    private IProductService productService;
    @Autowired
    private ICombDetailService combDetailService;
    @Autowired
    private ICombNumService combNumService;

    @ApiOperation(value = "获取套件列表信息", notes = "获取套件列表信息")
    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<CombDetail>> findCombDetails(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(required = false) Integer size,
            @RequestParam(required = false) String queryField,
            @RequestParam(required = false) String queryValue,
            @RequestParam(required = false) Boolean isAccurate,
            @RequestParam(defaultValue = "id") String orderBy,
            @RequestParam(defaultValue = "false") Boolean isAsc,
            @ModelAttribute("user") User operator) {

        Wrapper wrapper = MyBatisUtil.getIsActiveWrapper(queryField, queryValue, orderBy,isAsc);
        Page<CombDetail> page = combDetailService.selectPage(new Page<CombDetail>(current, size), wrapper);
        List<CombDetail> records = page.getRecords();

        for (CombDetail record : records) {
            combDetailService.getCombDetail(record);
        }
        return new PageResultMessage<>(records, combDetailService.selectCount(wrapper));
    }

    @ApiOperation(value = "获取单个套件信息", notes = "获取单个套件信息")
    @ApiImplicitParam(name = "id", value = "套件Id", required = true, dataType = "long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<CombDetail> findCombDetailById(@PathVariable long id){
        CombDetail combDetail = combDetailService.selectById(id);

        if (combDetail == null){
            throw new RestServiceException(HttpStatus.NOT_FOUND, ResultCode.ENTITY_NOT_FOUND);
        }

        combDetailService.getCombDetail(combDetail);

        return new ResultMessage<>(combDetail);
    }

    @ApiOperation(value="创建套件", notes="根据combDetail对象创建套件")
    @ApiImplicitParam(name = "combDetail", value = "套件详细实体", required = true, dataType = "CombDetail")
    @Transactional
    @RequestMapping(method= RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<CombDetail> insertCombDetail(@RequestBody CombDetail combDetail) {
        if(productService.selectById(combDetail.getProductId()) == null){
            throw new RestServiceException(ResultCode.CREATE_FAILED);
        }

        if(!combDetailService.insert(combDetail)){
            throw new RestServiceException(ResultCode.CREATE_FAILED);
        }

        for (CombNum combNum : combDetail.getCombNumList()){
            combNum.setCombDetailId(combDetail.getId());
        }

        if(!combNumService.insertBatch(combDetail.getCombNumList())){
            throw new RestServiceException(ResultCode.CREATE_FAILED);
        }

        return new ResultMessage<>(combDetail);
    }

    @ApiOperation(value="更新套件", notes="根据combDetail对象更新套件")
    @ApiImplicitParam(name = "combDetail", value = "套件详细实体", required = true, dataType = "CombDetail")
    @Transactional
    @RequestMapping(method=RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<CombDetail> updateCombDetail(@RequestBody CombDetail combDetail) {
        if(productService.selectById(combDetail.getProductId()) == null){
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }

        if(!combNumService.updateBatchById(combDetail.getCombNumList())){
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }

        combDetailService.updateById(combDetail);

        return new ResultMessage<>(combDetail);
    }


    @ApiOperation(value="批量删除套件", notes="根据url的ids来指定删除套件")
    @ApiImplicitParam(name = "ids", value = "套件Ids", required = true, dataType = "String")
    @Transactional
    @RequestMapping(value = "/{ids}",method=RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage deleteCombDetails(@PathVariable String ids) {
        List<Long> list = UtilityTool.convertStringArray2LongList(ids.split(","));
        for (Long id : list){
            if(!combDetailService.deleteCombDetailById(id)){
                throw new RestServiceException(ResultCode.DELETE_NOT_FOUND);
            }
        }
        return new ResultMessage();
    }

}
